<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="60px">
      <!-- <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-edit" @click="handAll">批量审核</el-button>
      </el-col> -->
      <el-form-item label="申请人姓名" prop="xm" label-width="100px">
        <el-input v-model="queryParams.xm" placeholder="请输入申请人姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="申请人身份证" prop="sfzhm" label-width="100px">
        <el-input v-model="queryParams.sfzhm" placeholder="请输入申请人身份证号码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="申请人学号" prop="xh" label-width="100px">
        <el-input v-model="queryParams.xh" placeholder="请输入申请人学号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="学院" prop="collegeId">
        <el-select @change="changeDept" v-model="queryParams.collegeId" placeholder="请选择学院" filterable clearable>
          <el-option v-for="(v, i) in deptList" :key="i" :label="v.label" :value="v.value"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="专业" prop="majorId">
        <el-select @change="changeMajor" v-model="queryParams.majorId" placeholder="请先选择学院再选择专业" filterable clearable>
          <el-option v-for="(v, i) in majorList" :key="i" :label="v.majorName" :value="v.majorId"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="班级" prop="classId">
        <el-select v-model="queryParams.classId" placeholder="请先选择专业再选择班级" filterable clearable>
          <el-option v-for="(v, i) in classList" :key="i" :label="v.className" :value="v.classId"> </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:apply:add']">新增</el-button>
        <el-button type="primary" icon="el-icon-edit" @click="handAll">批量审核</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="#" align="center" prop="id" />
      <el-table-column label="学年" align="center" prop="applyYear" />
      <el-table-column label="学院" align="center" prop="xy" />
      <!-- <el-table-column label="1：提交，2：班级" align="center" prop="step" /> -->
      <el-table-column label="申请人姓名" align="center" prop="xm" />
      <el-table-column label="性别" align="center" prop="xb" />
      <el-table-column label="民族" align="center" prop="mz" />
      <el-table-column label="身份证号码" align="center" prop="sfzhm" />
      <el-table-column label="学号" align="center" prop="xh" />
      <el-table-column label="班级" align="center" prop="bj" />
      <el-table-column label="出生年月" align="center" prop="csny" />
      <el-table-column label="二级学院审核意见" align="center" prop="xsqmyj">
        <template slot-scope="scope">
          <div v-if="scope.row.xsqmyj">
            <div v-if="scope.row.xsqmyj == '1'" style="color: #55aa00">特别困难</div>
            <div v-if="scope.row.xsqmyj == '2'" style="color: #55aa00">比较困难</div>
            <div v-if="scope.row.xsqmyj == '3'" style="color: #55aa00">一般困难</div>
            <div v-if="scope.row.xsqmyj == '4'" style="color: #ff5500">不困难</div>
          </div>
          <div v-else style="color: #ff0000">未审核</div>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-if="scope.row.step == 1" v-hasPermi="['system:apply:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-if="scope.row.step == 2" v-hasPermi="['system:apply:edit']">班级审核</el-button> -->
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">二级学院审核</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-if="scope.row.step == 4" v-hasPermi="['system:apply:edit']">学校审核意见</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改困难认定对话框 -->
    <el-dialog title="批量编辑" :visible.sync="open" width="80%" append-to-body>
      <el-form label-width="180px">
        <!-- <el-form-item label="困难类型">
          <el-radio-group v-model="form.ejxyldqmyj">
            <el-radio :label="'1'" name="type">特别困难</el-radio>
            <el-radio :label="'2'" name="type">比较困难</el-radio>
            <el-radio :label="'3'" name="type">一般困难</el-radio>
            <el-radio :label="'4'" name="type">不困难</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <el-form-item label="签字上传">
          <image-upload v-model="form.ejxyldqm" :disabled="true" :limit="1" />
        </el-form-item>
        <el-form-item label="签名日期">
          <el-col :span="11">
            <el-date-picker type="date" format="yyyy/MM/dd" value-format="yyyy/MM/dd" placeholder="选择日期" v-model="form.ejxyldqmrq" style="width: 100%"></el-date-picker>
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { isEmpty } from "@/api/helpFunc";
import { listClass } from "@/api/stuCQS/basedata/class";
import { listGrade } from "@/api/stuCQS/basedata/grade";
import { listMajors } from "@/api/stuCQS/basedata/majors";
import { getDeptName } from "@/api/system/dept";
import { getUserProfile, updateSignature } from '@/api/system/user';
import { delApply, listXw as listApply, updateAllSrsKnrdApply } from '@/api/zxj/poverty/apply';
import { Message } from 'element-ui';
export default {
  name: 'Apply',
  data() {
    return {
      user: { signature: null },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 困难认定表格数据
      applyList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        step: null,
        xm: null,
        xb: null,
        mz: null,
        sfzhm: null,
        xh: null,
        csny: null,
        knlx: null,
        knlx2: null,
        rkzs: null,
        srly: null,
        hjxz: null,
        hjszd: null,
        jtcy: null,
        jtjzfqk: null,
        jtqcqk: null,
        jtxjzdz: null,
        jzdh: null,
        jtnsr: null,
        rjnsr: null,
        jtzysr: null,
        ssldnl: null,
        hbcjr: null,
        zyssldnl: null,
        tfsj: null,
        sqrqm: null,
        sqrqmrq: null,
        bjpyyj: null,
        pyxzzzqm: null,
        pyxzzzqmrq: null,
        fdyqm: null,
        fdyqmrq: null,
        ejxyldqmyj: null,
        ejxyldqm: null,
        ejxyldqmrq: null,
        xsqmyj: null,
        xsqm: null,
        xsqmrq: null,
        createId: null,
      },
      // 表单参数
      form: {
        step: 4,
        ejxyldqmyj: '',
        ejxyldqm: '',
        ejxyldqmrq: '',
      },
      // 表单校验
      rules: {
        fdyqmrq: [
          {
            required: true,
            message: '辅导员签名日期不能为空',
            trigger: 'blur',
          },
        ],
      },
      deptList: [], //学院列表
      majorList: [], //专业列表
      classList: [], //班级列表
      gradeList: [], //年级列表
    }
  },
  created() {
    this.listDept();
    this.listGrade();
    this.getList()
    getUserProfile().then((response) => {
      this.form.ejxyldqm = response.data.signature
    })
  },
  methods: {
    async listGrade() {
      let res = await listGrade();
      this.gradeList = [...res.rows];
    },
    async changeMajor() {
      if (!isEmpty(this.queryParams.majorId)) {
        let sdata = {
          pageNum: 1,
          pageSize: 100,
          majorId: this.queryParams.majorId
        }
        let res = await listClass(sdata);
        this.classList = [...res.rows];
      } else {
        this.queryParams.classId = null;
        this.classList = [];
      }

    },
    async changeDept() {
      if (!isEmpty(this.queryParams.collegeId)) {
        let sdata = {
          collegeId: this.queryParams.collegeId,
          pageNum: 1,
          pageSize: 100
        }
        let res = await listMajors(sdata);
        this.majorList = [...res.rows];
      } else {
        this.queryParams.majorId = null;
        this.majorList = [];
      }
    },
    async listDept() {
      let res = await getDeptName();
      this.deptList = [...res.data];
    },
    handAll() {
      if (this.ids.length == 0) {
        Message({
          message: '请选择',
          type: 'error',
        })
        return
      }

      this.open = true
    },
    /** 查询困难认定列表 */
    getList() {
      this.loading = true
      listApply(this.queryParams).then((response) => {
        this.applyList = response.rows
        console.log(this.applyList)
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        ejxyldqmyj: '',
        ejxyldqm: '',
        ejxyldqmrq: '',
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({
        path: '/hard/pks/poverty/edit',
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const applylists = this.applyList
      const id = row.id || this.ids
      localStorage.setItem('knList', JSON.stringify(applylists))
      this.$router.push({
        path: '/hard/pks/poverty/edit',
        query: {
          id: id,
          // she: 0
        },
      })
      // getApply(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改困难认定";
      // });
    },
    doUpdateSignature() {
      updateSignature(this.user).then((res) => {})
    },
    /** 提交按钮 */
    submitForm() {
      if (this.form.ejxyldqm == '') {
        Message({
          message: '请上传签名文件',
          type: 'error',
        })
        return
      }
      if (this.form.ejxyldqmrq == '') {
        Message({
          message: '请选择签名日期',
          type: 'error',
        })
        return
      }
      this.form.ejxyldqmyj = 'bjpyyi'

      this.form.step = 4
      this.form.ids = this.ids
      updateAllSrsKnrdApply(this.form).then((response) => {
        if (response.code == 200) {
          this.$modal.msgSuccess('批量修改成功')
          this.open = false
          this.doUpdateSignature()
          this.reset()
          this.getList()
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认删除困难认定编号为"' + ids + '"的数据项？')
        .then(function () {
          return delApply(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'system/apply/export',
        {
          ...this.queryParams,
        },
        `apply_${new Date().getTime()}.xlsx`
      )
    },
  },
}
</script>
